//
//  User.swift
//  Employee
//
//  Created by swordjoy on 2017/4/1.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import Foundation
import SwiftyJSON

var Avatar_Key = "Avatar_Key.swordjoy" + Account.userAccount
var HuanxinAccount_Key = "HuanxinAccount_Key.swordjoy" + Account.userAccount
var HuanxinPassword_Key = "HuanxinPassword_Key.swordjoy" + Account.userAccount
var Name_Key = "Name_Key.swordjoy" + Account.userAccount
var Organization_Key = "Organization_Key.swordjoy" + Account.userAccount
var Phone_Key = "Phone_Key.swordjoy" + Account.userAccount
var StaffId_Key = "StaffId_Key.swordjoy" + Account.userAccount


/// 用来存储用户个人信息
struct User {
  
  static var cacheFileName: URL {
    return SJApp.cachesURL.appendingPathComponent("UserInfo.plist")
  }
  
  static var userInfoDic: NSMutableDictionary? {
    return NSMutableDictionary(contentsOf: User.cacheFileName)
  }
  
  static var avatar: String {
    get {
      return (User.userInfoDic?.object(forKey: "useravatar") as? String) ?? ""
    }
    set {
      let tempDic = User.userInfoDic!
      tempDic.setObject(newValue, forKey: "useravatar" as NSString)
      tempDic.write(to: User.cacheFileName, atomically: true)
    }
  }
  
  static var huanxinAccount: String {
    return (User.userInfoDic?.object(forKey: "userhuanxinAccount") as? String) ?? ""
  }
  
  static var huanxinPassword: String {
    return (User.userInfoDic?.object(forKey: "userhuanxinPassword") as? String) ?? ""
  }
  
  static var name: String {
    return (User.userInfoDic?.object(forKey: "username") as? String) ?? ""
  }
  
  static var organization: String {
    return (User.userInfoDic?.object(forKey: "userorganization") as? String) ?? ""
  }
  
  static var phone: String {
    return (User.userInfoDic?.object(forKey: "userphone") as? String) ?? ""
  }
  
  static var staffId: String {
    return (User.userInfoDic?.object(forKey: "userstaffId") as? String) ?? ""
  }
    
    // 这里我提交的
    static var orgId: String {
        return (User.userInfoDic?.object(forKey: "orgId") as? String) ?? ""
    }
  
  static var tags: [String] {
    let tagsString = (User.userInfoDic?.object(forKey: "pushTags") as? String) ?? ""
    let tagsArray = tagsString.components(separatedBy: ",")
    return tagsArray
  }
  
  static func save(with json: JSON) {
    
    let userDic = NSMutableDictionary()
    
    userDic.setObject(json["avatar"].stringValue, forKey: "useravatar" as NSString)
    userDic.setObject(json["huanxinAccount"].stringValue, forKey: "userhuanxinAccount" as NSString)
    userDic.setObject(json["huanxinPassword"].stringValue, forKey: "userhuanxinPassword" as NSString)
    userDic.setObject(json["name"].stringValue, forKey: "username" as NSString)
    userDic.setObject(json["organization"].stringValue, forKey: "userorganization" as NSString)
    userDic.setObject(json["phone"].stringValue, forKey: "userphone" as NSString)
    userDic.setObject(json["staffId"].stringValue, forKey: "userstaffId" as NSString)
    userDic.setObject(json["pushTags"].stringValue, forKey: "pushTags" as NSString)
    userDic.setObject(json["orgId"].stringValue, forKey: "orgId" as NSString)
    
    userDic.write(to: User.cacheFileName, atomically: true)
    
  }
  
  static func clearAll() {
    let clearDic = NSDictionary()
    clearDic.write(to: User.cacheFileName, atomically: true)
  }
    static func logoutClean() {
        SJCache.clearAll()
        Account.isLogin = false
        JPUSHService.setTags(Set(), alias: "", fetchCompletionHandle: nil)
    }
    
    static func otherDeviceLogin() {
        let loginVC = SJBaseNavigationC(rootViewController: LoginVC())
        UIApplication.shared.keyWindow?.rootViewController?.present(loginVC, animated: true, completion: {
            
        })
    }
    
    static func longTimeNotLogin() {
        let loginVC = SJBaseNavigationC(rootViewController: LoginVC())
        UIApplication.shared.keyWindow?.rootViewController?.present(loginVC, animated: true, completion: {
            
        })
    }
  
  
}

let UserAccount_Key = "UserAccount_Key.swordjoy"
let IsLogin_Key = "IsLogin_Key.swordjoy"
let Is_FromLoginVC_Key = "isFromLoginVC_Key"
let IsSetPushAlias_Key = "isSetPushAlias_Key"
let IsNotice_key = "IsNotice_key"

struct Account {
  
  static func clearAll() {
    let clearDic = NSMutableDictionary()
    let account = Account.userAccount
    clearDic.setObject(account, forKey: UserAccount_Key as NSString)
    clearDic.write(to: Account.cacheFileName, atomically: true)
  }
  
  static var cacheFileName: URL {
    
    return SJApp.cachesURL.appendingPathComponent("Account.plist")
    
  }
  
  static var accountInfoDic: NSMutableDictionary {
    
    return NSMutableDictionary(contentsOf: Account.cacheFileName) ?? NSMutableDictionary()
    
  }
  
  static var userAccount: String {
    get {
      return (Account.accountInfoDic.object(forKey: UserAccount_Key) as? String) ?? ""
    }
    set {
      let dic = Account.accountInfoDic
      dic.setObject(newValue, forKey: UserAccount_Key as NSString)
      dic.write(to: Account.cacheFileName, atomically: true)
    }
  }
  
  static var isSetPushAlias: Bool {
    get {
      return (Account.accountInfoDic.object(forKey: IsSetPushAlias_Key) as? Bool) ?? false
    }
    set {
      let dic = Account.accountInfoDic
      dic.setObject(newValue, forKey: IsSetPushAlias_Key as NSString)
      dic.write(to: Account.cacheFileName, atomically: true)
    }
  }
  
    static var isNotice: Bool {
        get {
            return (Account.accountInfoDic.object(forKey: IsNotice_key) as? Bool) ?? false
        }
        set {
            let dic = Account.accountInfoDic
            dic.setObject(newValue, forKey: IsNotice_key as NSString)
            dic.write(to: Account.cacheFileName, atomically: true)
        }
    }
  
  static var isLogin: Bool {
    get {
      return (Account.accountInfoDic.object(forKey: IsLogin_Key) as? Bool) ?? false
    }
    set {
      let dic = Account.accountInfoDic
      dic.setObject(newValue, forKey: IsLogin_Key as NSString)
      dic.write(to: Account.cacheFileName, atomically: true)
    }
  }
  
}

